package com.start_eternity.oasissafewalk.controller;
//修改记录查询
import com.start_eternity.oasissafewalk.pojo.PageBean;
import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.service.LoggService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

@Slf4j
@RestController
@EnableScheduling
@RequestMapping("/logg")
public class LoggController {
    @Autowired
    private LoggService loggService;

    //分页查询
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,//可以根据id，loggSize，name查询操作记录
                       @RequestParam(defaultValue = "10") Integer pageSize, Integer id,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate loggTime, String name){
        log.info("分页查询所有操作记录，参数：{},{},{},{},{}",page,pageSize,id,loggTime,name);
        PageBean pageBean = loggService.page(page,pageSize,id,loggTime,name);
        return Result.success(pageBean);
    }
    //定期删除操作0 0 0 L * ?
    @Scheduled(cron = "0 0 0 1 * *")//每月的第一天执行i9
    public Result delete() throws Exception {
        //将当前时间赋值给time与以前的时间进行比较
        LocalDateTime times = LocalDateTime.now();//获取当前时间
        Instant instant = times.toInstant(ZoneOffset.UTC);//将其转换为带有系统默认时区信息的Instant对象
        //Instant对象本身并不包含时区信息，它代表的是一个不受时区影响的时间点
        long time = instant.toEpochMilli();//将Instant对象转换为毫秒值
        System.out.println("当前毫秒值："+time);
        System.out.println("开始根据time执行清除操作"+times);
        loggService.delete(time);
        return Result.success();
    }
}
